﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using EggOfPanKu.Library.Objects;

namespace DeckBuilder
{
    public class Deck
    {
        private ObservableCollection<DeckCard> _CardCollection;
        public ObservableCollection<DeckCard> CardCollection
        {
            get { return _CardCollection; }
            set { _CardCollection = value; }
        }

        public Deck()
        {
            CardCollection = new ObservableCollection<DeckCard>();
        }

        public void AddCard(Card card)
        {
            bool contains = false;
            foreach (DeckCard dc in CardCollection)
            {
                if (dc.CardInDeck == card)
                {
                    dc.Count++;
                    contains = true;
                }
            }

            if (!contains)
            {
                DeckCard dc = new DeckCard();
                dc.CardInDeck = card;
                dc.Count = 1;

                CardCollection.Add(dc);
            }
        }

        public void RemoveCard(Card card)
        {
            foreach (DeckCard dc in CardCollection)
            {
                if (dc.CardInDeck == card)
                {
                    if (dc.Count > 1)
                    {
                        dc.Count--;
                        break;
                    }
                    else
                    {
                        CardCollection.Remove(dc);
                        break;
                    }
                }
            }
        }

        public string WriteOutDeck()
        {

            StringBuilder strongholdCard = new StringBuilder("# StrongHold" + System.Environment.NewLine);
            StringBuilder fateCards = new StringBuilder("# Fate" + System.Environment.NewLine);
            StringBuilder dynastyCards = new StringBuilder("# Dynasty" + System.Environment.NewLine);

            //For Now, I'm rebuilding the String each time this is called.  Decks will be small, so this should be relatively inexpensive.  
            // Obviously, making this more effecient would be ideal.
            for (int i = 0; i < CardCollection.Count; i++)
            {
                if (CardCollection[i].CardInDeck.DeckType == CardDeck.Stronghold)
                {
                    strongholdCard.Append(CardCollection[i].Count.ToString() + " " + CardCollection[i].CardInDeck.Name + System.Environment.NewLine);
                }
                if (CardCollection[i].CardInDeck.DeckType == CardDeck.Fate)
                {
                    fateCards.Append(CardCollection[i].Count.ToString() + " " + CardCollection[i].CardInDeck.Name + System.Environment.NewLine);
                }
                if (CardCollection[i].CardInDeck.DeckType == CardDeck.Dynasty)
                {
                    dynastyCards.Append(CardCollection[i].Count.ToString() + " " + CardCollection[i].CardInDeck.Name + System.Environment.NewLine);
                }
            }

            return strongholdCard.ToString() + System.Environment.NewLine + dynastyCards.ToString() + System.Environment.NewLine + fateCards.ToString();
        }

        public void LoadDeckFile(string deckFilePath, ObservableCollection<Card> cardDB)
        {

            // EggOfPanKu.Library.Objects.CardDatabase.GetCardByName(cardDB, "");
        }

    }

    public class DeckCard
    {
        private int _Count;
        public int Count
        {
            get { return _Count; }
            set { _Count = value; }
        }

        private Card _CardInDeck;
        public Card CardInDeck
        {
            get { return _CardInDeck; }
            set { _CardInDeck = value; }
        }
    }
}
